<?php


namespace app\api\controller;


use app\Request;
use app\api\model\Wxnumber;
use liliuwei\think\Jump;
use think\facade\Cache;
use think\facade\Db;
class Wx
{
use Jump;


    /**
     * Index constructor.
     * 解决api跨域问题
     */
    public function __construct(){
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, X_Requested_With,Content-Type, Accept");
        header('Access-Control-Allow-Methods: POST,GET,PUT');
        if(request()->isOptions()){
            exit();
        }
    }
    /**
     * 获取当前组的微信号
     */
    public function catchOne(Request $request){

        $gid = $request->param('gid');
        $uid = $request->param('uid');
        // 先查询redis是否存在当前分组微信号
        $redisGid = Cache::store('redis')->get($gid);
        if (empty($redisGid)) {
            // 根据微信分组显示，暂时不上线此功能
            // $wxgroupStatus = Db::table('Wxgroup')->where(['id'=>$gid,'status'=>1])->find();
            // if (empty($wxgroupStatus)) {
            //     return wx_msg('0',$wxgroupStatus);
            // }
            // 从数据库读取微信号分组
            $wxnumber = new Wxnumber();
            $data =  $wxnumber->where(['uid'=>$uid,'gid'=>$gid])->where('status',1)->withoutField('id,uid,gid,create_time,update_time')->select()->toArray();
            if (!empty($data)) {
                // 将获取到的微信号存储到redis
                Cache::store('redis')->set($gid,$data,3600);
                return wx_msg('0',$data);
            }else{
                return wx_msg('0','非法数据！！！');
            }
            
        }else{

            return wx_msg('0',$redisGid);
        
        }
        
        
    }
}
